﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "CmdUseGRepHolder.h"
#include "IUiView.h"
#include "IElement.h"
#include "Matrix4d.h"
#include "IDocument.h"
#include "ElementId.h"
#include "ISelection.h"
#include "IModelView.h"
#include "IUiDocument.h"
#include "IDirectShape.h"
#include "IGraphicsNode.h"
#include "UiCommonDialog.h"
#include "IGenericElement.h"
#include "GcmpCommandState.h"
#include "IUserTransaction.h"
#include "IElementModelShape.h"
#include "IPureGraphicsElement.h"
#include "IGraphicsElementShape.h"
#include "IUiDocumentViewManager.h"
#include "IGraphicsNodePathCollector.h"
#include "IGraphicsElementShapeReference.h"

#include "GRepHolder.h"
#include <unordered_set>
#include "GRepCommandIds.h"
#include "GRepHolderUser.h"
#include "CommandRegister.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

REGISTER_COMMAND(CmdUseGRepHolder)

Sample::CmdUseGRepHolder::CmdUseGRepHolder()
    : CommandBase(ID_CMD_USE_GREP_HOLDER)
{
}

bool Sample::CmdUseGRepHolder::IsEnabled() const
{
    // 需要在项目环境中，并且有一个图元被选中
    if (!GcmpCommandState::IsInDocumentEnvironment())
    {
        return false;
    }
    if (ISelection* pSelection = ISelection::Get())
    {
        std::unordered_set<ElementId, ElementIdHash> elemMaps = pSelection->GetSelectedElements();
        if (elemMaps.size() == 1)
        {
            return true;
        }
    }
    return false;
}

bool Sample::CmdUseGRepHolder::IsVisible() const
{
    return GcmpCommandState::IsInDocumentEnvironment();
}

gcmp::OwnerPtr<gcmp::IAction> Sample::CmdUseGRepHolder::ExecuteCommand(const gcmp::CommandParameters & cmdParams)
{
    IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocViewMgr, L"获取UI文档视图管理器失败",L"GDMP",L"2024-03-30");
    IUiDocument* pUiDoc = pUiDocViewMgr->GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDoc, L"获取UI文档失败",L"GDMP",L"2024-03-30");
    IDocument* pDoc = pUiDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"获取文档失败",L"GDMP",L"2024-03-30");
    IUiView* pCurrentView = pUiDocViewMgr->GetCurrentUiView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pCurrentView, L"pCurrentView为空",L"GDMP",L"2024-03-30");
    IModelView* pModelView = pCurrentView->GetModelView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMP",L"2024-03-30");

    ISelection* pSelection = ISelection::Get();
    std::unordered_set<ElementId, ElementIdHash> elemMaps = pSelection->GetSelectedElements();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(elemMaps.size() > 0, L"elemMaps为空",L"GDMP",L"2024-03-30");

    ElementId id = *elemMaps.begin();
    IElement* pElement = pDoc->GetElement(id);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElement, L"pElement为空",L"GDMP",L"2024-03-30");

    IGenericElement* pGenericElement = quick_cast<IGenericElement>(pElement);
    if (nullptr == pGenericElement)
    {
        UiCommonDialog::ShowMessageBox(L"提示", L"请先运行“创建GRepHolder”，并选中创建的GRepHolder", (int)UiCommonDialog::ButtonType::Yes);
        return nullptr;
    }
    GRepHolder* pGRepHolder = quick_cast<GRepHolder>(pGenericElement->GetExternalObject());
    if (nullptr == pGRepHolder)
    {
        UiCommonDialog::ShowMessageBox(L"提示", L"请先运行“创建GRepHolder”，并选中创建的GRepHolder", (int)UiCommonDialog::ButtonType::Yes);
        return nullptr;
    }

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, L"从选择GRepHolder创建IPureGraphicsElement");
    // 在原来GRepHolder偏离一米的位置创建GRepHolderUser，从而可以和区分两者
    Matrix4d matrix;
    matrix.MakeTranslate(Vector3d(1000, 0, 0));
    OwnerPtr<IGraphicsElementShapeReference> opGNodeRef = IGraphicsElementShapeReference::Create(pGenericElement->GetElementId(), matrix);
    GRepHolderUser* pGRepHolderUser = GRepHolderUser::Create(pDoc, TransferOwnership(opGNodeRef));
    opTransaction->Commit();

    return nullptr;
}

